--
-- Author: shunguo.chen
-- Date: 2018-06-25 19:39:34
--
-- SkillSimpleRender 简单技能图标，对 Res.BaseSkillSimple 的解析
--

local ClassRef = app.mvc.createRenderClass(...)

function ClassRef.createWithData(data, node)
    local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.BaseSkillSimple))
    if node == nil then
        ccui.Helper:doLayout(sprite)
    end
    sprite:init(data)
    return sprite
end
function ClassRef.create(node)
    return ClassRef.createWithData(nil, node)
end

-- data = Skill
function ClassRef:init(data)
    self.super.init(self, data)

    self._ui.icon:setTouchEnabled(true)
    display.uiAddClick(self._ui.icon, function(sender, eventType)
            self:showTip()
        end)
    self:refresh()
end

function ClassRef:refresh()
    if self._data then
        self._ui.icon:loadTexture(self._data:getIcon())
    end
end

function ClassRef:setTipEnable(b)
    self._tipEnable = b
end

function ClassRef:showTip()
    if self._tipEnable and self._data then
        local icon = self._ui.icon
        local boundingBox = icon:getBoundingBox()
        local worldPos = icon:convertToWorldSpace(cc.p(0, 0))
        boundingBox.x = worldPos.x
        boundingBox.y = worldPos.y
        local params = {
            tipsType = Enums.TipsType.classSkill,
            dataTemp = self._data,
            boundingBox = boundingBox
        }
        app:dispatchCustomEvent("EVENT_OPEN_FLOAT", {params=params})
    end
end

function ClassRef:onBtnClick(cb)
    self._onClick = cb
end

return ClassRef
